<?php

include '../../../configuration.php';

if ($_POST['category'] != null) {
/*echo "yes";*/
	$category_title = $_POST['category'];
	$result = mysql_query("SELECT * FROM `categories` WHERE `cat_title` = '$category_title'");
	$row = mysql_fetch_assoc($result);
	$category_id = $row["cat_id"];
	
	$question_id = array();
	$result2 = mysql_query("SELECT * FROM `questions` WHERE `cat_id` = $category_id");
	while ($rows = mysql_fetch_array($result2,MYSQL_ASSOC)){
		$question_id[] = $rows["q_id"];
	}
	
	mysql_query("DELETE FROM `categories` WHERE `cat_id`=$category_id");
	mysql_query("DELETE FROM `questions` WHERE `cat_id`=$category_id");
	
	for($i = 0; $i < count($question_id); $i++){
		mysql_query("DELETE FROM `choices` WHERE `q_id`=$question_id[$i]");
	}
/*echo "<pre>";
echo $category_id;
print_r($question_id);
echo "</pre>";*/
}
?>

<?php

$smarty = new Smarty();

$smarty->template_dir = $_SERVER['DOCUMENT_ROOT'].'/sdupgk/administrator/main/templates/';
$smarty->compile_dir = $_SERVER['DOCUMENT_ROOT'].'/sdupgk/administrator/main/templates_c/';
$smarty->config_dir = $_SERVER['DOCUMENT_ROOT'].'/sdupgk/administrator/main/configs';

$smarty->compile_check = true;
//$smarty->debugging = true;

$result = mysql_query("SELECT * FROM categories");

$cat_title = array();

while ($rows = mysql_fetch_array($result, MYSQL_ASSOC)) {
	$cat_title[] = "<option>".$rows["cat_title"]."</option>";
}

$smarty->assign('categories',$cat_title);
include '../../check.php';
if ($checking == 1)
	$smarty->display("delete_category.tpl");
else echo "You are not autorised"; exit;
?>